// 轮播数据配置
const carouselData = [
    {
      imgSrc: "./images/banner1.png",
      alt: "考试倒计时",
      title: "距离计算机二级考试仅剩XX天！",
      text: "立即开始备考，轻松通过考试！",
      btnText: "开始练习",
      isCountdown: true
    },
    {
      imgSrc: "./images/banner2.png",
      alt: "核心功能",
      title: "海量题库，精准解析！",
      text: "覆盖MS Office、Python、C语言等科目。",
      btnText: "进入题库"
    },
    {
      imgSrc: "./images/banner3.png",
      alt: "热门活动",
      title: "考前冲刺班火热报名中！",
      text: "限时优惠，名额有限，立即报名！",
      btnText: "了解更多"
    }
  ];
  
  // 初始化轮播组件
  function initCarousel() {
    const carouselInner = document.querySelector('.carousel-inner');
    const indicators = document.querySelector('.carousel-indicators');
    let currentIndex = 0;
  
    // 生成轮播项
    carouselData.forEach((item, index) => {
      // 创建轮播项
      const carouselItem = document.createElement('div');
      carouselItem.className = `carousel-item ${index === 0 ? 'active' : ''}`;
      
      // 创建图片
      const img = document.createElement('img');
      img.src = item.imgSrc;
      img.alt = item.alt;
      img.className = 'd-block w-100';
      
      // 创建字幕
      const caption = document.createElement('div');
      caption.className = 'carousel-caption';
      
      const title = document.createElement('h2');
      title.textContent = item.title;
      
      const text = document.createElement('p');
      text.textContent = item.text;
      
      const button = document.createElement('button');
      button.textContent = item.btnText;
      
      // 组装元素
      caption.appendChild(title);
      caption.appendChild(text);
      caption.appendChild(button);
      carouselItem.appendChild(img);
      carouselItem.appendChild(caption);
      carouselInner.appendChild(carouselItem);
  
      // 创建指示器
      const indicator = document.createElement('span');
      indicator.className = `indicator ${index === 0 ? 'active' : ''}`;
      indicator.addEventListener('click', () => goToSlide(index));
      indicators.appendChild(indicator);
    });
  
    // 倒计时初始化
    if (carouselData[0].isCountdown) {
      updateCountdown();
      setInterval(updateCountdown, 1000 * 60 * 60); // 每小时更新
    }
  }
  
  // 倒计时功能
  function updateCountdown() {
    const examDate = new Date('2024-03-25');
    const now = new Date();
    const diff = examDate - now;
    
    const days = Math.floor(diff / (1000 * 60 * 60 * 24));
    const title = document.querySelector('.carousel-item:first-child h2');
    if (title) {
      title.textContent = `距离计算机二级考试仅剩${days}天！`;
    }
  }
  
  // 轮播控制函数
  let currentSlide = 0;
  const totalSlides = document.querySelectorAll('.carousel-item').length;
  
  function prevSlide() {
    currentSlide = (currentSlide - 1 + totalSlides) % totalSlides;
    updateCarousel();
  }
  
  function nextSlide() {
    currentSlide = (currentSlide + 1) % totalSlides;
    updateCarousel();
  }
  
  function goToSlide(index) {
    currentSlide = index;
    updateCarousel();
  }
  
  function updateCarousel() {
    const items = document.querySelectorAll('.carousel-item');
    const indicators = document.querySelectorAll('.indicator');
    
    items.forEach((item, index) => {
      item.classList.toggle('active', index === currentSlide);
      indicators[index].classList.toggle('active', index === currentSlide);
    });
  }
  
  // 自动播放
  let autoPlay = setInterval(nextSlide, 5000);
  
  // 暂停/恢复自动播放
  document.querySelector('.carousel').addEventListener('mouseenter', () => {
    clearInterval(autoPlay);
  });
  
  document.querySelector('.carousel').addEventListener('mouseleave', () => {
    autoPlay = setInterval(nextSlide, 5000);
  });
  
  // 初始化组件
  document.addEventListener('DOMContentLoaded', initCarousel);